<template>
	<el-drawer
		v-model="dialogVisible"
		:destroy-on-close="true"
		size="80%"
		class="order-detail-drawer"
	>
		<template #header>
			<div class="drawer-header">
				<div class="title">订单详情</div>
				<div class="order-no">
					<el-tag size="large">{{ detail.orderNo }}</el-tag>
				</div>
			</div>
		</template>

		<div class="order-detail">
			<!-- 基础信息 -->
			<div class="detail-section">
				<div class="section-title">基础信息</div>
				<el-descriptions :column="3" border>
					<el-descriptions-item label="客户名称">
						{{ detail.customerName }}
					</el-descriptions-item>
					<el-descriptions-item label="客户编号">
						{{ detail.customerNo }}
					</el-descriptions-item>
					<el-descriptions-item label="发运类型">
						{{ detail.shipType }}
					</el-descriptions-item>
					<el-descriptions-item label="目的地">
						{{ detail.destination }}
					</el-descriptions-item>
					<el-descriptions-item label="下单时间">
						{{ detail.orderTime }}
					</el-descriptions-item>
					<el-descriptions-item label="订单状态">
						<el-tag :type="getStatusType(detail.orderStatus)">
							{{ detail.orderStatus }}
						</el-tag>
					</el-descriptions-item>
				</el-descriptions>
			</div>

			<!-- 运单信息 -->
			<div class="detail-section">
				<div class="section-title">运单信息</div>
				<WaybillTimeline
					:waybill-list="detail.waybillList"
					@select="handleSelectWaybill"
				/>
			</div>

			<!-- 发货信息 -->
			<div class="detail-section">
				<div class="section-title bg-[#fff3e8]">发货信息</div>
				<el-descriptions :column="3" border>
					<el-descriptions-item label="联系人">
						{{ detail.senderName }}
					</el-descriptions-item>
					<el-descriptions-item label="联系电话">
						{{ detail.senderPhone }}
					</el-descriptions-item>
					<el-descriptions-item label="地址编码">
						{{ detail.senderAddressCode }}
					</el-descriptions-item>
					<el-descriptions-item label="详细地址" :span="3">
						{{ detail.senderAddress }}
					</el-descriptions-item>
					<el-descriptions-item label="计划发货时间" :span="3">
						{{ detail.planSendTime }}
					</el-descriptions-item>
				</el-descriptions>
			</div>

			<!-- 收货信息 -->
			<div class="detail-section">
				<div class="section-title bg-[#e8f7f3]">收货信息</div>
				<el-descriptions :column="3" border>
					<el-descriptions-item label="联系人">
						{{ detail.receiverName }}
					</el-descriptions-item>
					<el-descriptions-item label="联系电话">
						{{ detail.receiverPhone }}
					</el-descriptions-item>
					<el-descriptions-item label="地址编码">
						{{ detail.receiverAddressCode }}
					</el-descriptions-item>
					<el-descriptions-item label="详细地址" :span="3">
						{{ detail.receiverAddress }}
					</el-descriptions-item>
					<el-descriptions-item label="计划收货时间" :span="3">
						{{ detail.planReceiveTime }}
					</el-descriptions-item>
				</el-descriptions>
			</div>

			<!-- 货物信息 -->
			<div class="detail-section">
				<div class="section-title">货物信息</div>
				<el-table :data="detail.goodsList" border>
					<el-table-column
						type="index"
						label="序号"
						width="60"
						align="center"
					/>
					<el-table-column prop="name" label="货物名称" min-width="150" />
					<el-table-column
						prop="quantity"
						label="数量"
						width="150"
						align="right"
					/>
					<el-table-column prop="unit" label="单位" width="150" />
					<el-table-column
						prop="requiredTimes"
						label="需求车次"
						width="150"
						align="right"
					/>
				</el-table>
			</div>

			<!-- 费用信息 -->
			<div class="detail-section">
				<div class="section-title">费用信息</div>
				<el-descriptions :column="3" border>
					<el-descriptions-item label="运费总计">
						{{ detail.totalFreight }} 元
					</el-descriptions-item>
					<el-descriptions-item label="基础运费">
						{{ detail.baseFreight }} 元
					</el-descriptions-item>
					<el-descriptions-item label="加急费用">
						{{ detail.urgentFee }} 元
					</el-descriptions-item>
				</el-descriptions>
			</div>

			<!-- 订单备注 -->
			<div class="detail-section">
				<div class="section-title">订单备注</div>
				<div class="remark-content">{{ detail.remark || '暂无备注' }}</div>
			</div>

			<!-- 操作日志 -->
			<div class="detail-section">
				<div class="section-title">操作日志</div>
				<OperationLog :log-list="detail.operationLogs" />
			</div>
		</div>

		<template #footer>
			<div class="drawer-footer">
				<el-button @click="handleClose">关闭</el-button>
				<el-button type="primary" @click="handleEdit">编辑</el-button>
			</div>
		</template>
	</el-drawer>
</template>

<script setup>
import { ref, watch } from 'vue';
import {
	Check,
	TakeawayBox,
	Document,
	Edit,
	CircleClose,
	Warning,
} from '@element-plus/icons-vue';
import WaybillTimeline from './WaybillTimeline.vue';
import OperationLog from './OperationLog.vue';

const props = defineProps({
	modelValue: {
		type: Boolean,
		default: false,
	},
	detailData: {
		type: Object,
		default: () => ({}),
	},
});

const emit = defineEmits(['update:modelValue', 'edit']);

const dialogVisible = ref(false);
const detail = ref({
	orderNo: '',
	customerName: '',
	customerNo: '',
	shipType: '',
	destination: '',
	orderTime: '',
	senderName: '',
	senderPhone: '',
	senderAddressCode: '',
	senderAddress: '',
	planSendTime: '',
	receiverName: '',
	receiverPhone: '',
	receiverAddressCode: '',
	receiverAddress: '',
	planReceiveTime: '',
	goodsList: [],
	totalFreight: 0,
	baseFreight: 0,
	urgentFee: 0,
	remark: '',
	orderStatus: '',
	orderSource: '',
	creator: '',
	createTime: '',
	// 运单列表
	waybillList: [
		{
			waybillNo: 'YD20250501001',
			operator: '张三',
			operateTime: '2025-5-1 10:22:20',
			weight: '48.94',
			netWeight: '30.12',
			status: '已完成',
			selected: true,
		},
		{
			waybillNo: 'YD20250501002',
			operator: '李四',
			operateTime: '2025-5-1 08:22:20',
			weight: '18.82',
			netWeight: '30.12',
			status: '进行中',
			selected: false,
		},
	],
	// 操作日志
	operationLogs: [
		{
			type: '取消订单',
			operator: '迎曦',
			content: '取消原因说明',
			operateTime: '2016-09-21 08:50:08',
			remark: '这是一条描述，关于这个应用的描述',
		},
		{
			type: '修改货物信息',
			operator: '海纳',
			content: '重量从100吨变更为200吨',
			operateTime: '2016-09-20 08:50:08',
			remark: '这是一条描述，关于这个应用的描述',
		},
		{
			type: '订单加急',
			operator: '王大锤',
			content: '期望送达时间：2025-5-1 10:00:00',
			operateTime: '2016-09-19 08:50:08',
			remark: '关于这个应用的描述',
		},
		{
			type: '修改收发货地址',
			operator: '小王子',
			content: '收货地址由A变更为B',
			operateTime: '2016-09-18 08:50:08',
			remark: '关于这个应用的描述',
		},
		{
			type: '创建订单',
			operator: '皇后娘娘',
			content: '',
			operateTime: '2016-09-17 08:50:08',
			remark: '这是一条描述，关于这个应用的描述',
		},
	],
});

// 监听弹窗显示状态
watch(
	() => props.modelValue,
	(val) => {
		dialogVisible.value = val;
		if (val && props.detailData) {
			detail.value = {
				...detail.value,
				...props.detailData,
			};
		}
	},
);

// 监听弹窗关闭
watch(dialogVisible, (val) => {
	emit('update:modelValue', val);
});

// 获取状态类型
const getStatusType = (status) => {
	const statusMap = {
		已确认: 'success',
		已派车: 'warning',
		已装车: 'warning',
		已签收: 'info',
		已完成: '',
	};
	return statusMap[status] || 'info';
};

// 获取日志图标
const getLogIcon = (type) => {
	const iconMap = {
		create: Document,
		edit: Edit,
		dispatch: TakeawayBox,
		modify: Warning,
		delete: CircleClose,
		complete: Check,
	};
	return iconMap[type] || Document;
};

// 获取日志图标样式
const getLogIconClass = (type) => {
	const classMap = {
		create: 'create',
		edit: 'edit',
		dispatch: 'dispatch',
		modify: 'modify',
		delete: 'delete',
		complete: 'complete',
	};
	return classMap[type] || 'default';
};

// 选择运单
const handleSelectWaybill = (waybill) => {
	detail.value.waybillList.forEach((item) => {
		item.selected = item.waybillNo === waybill.waybillNo;
	});
};

// 关闭
const handleClose = () => {
	dialogVisible.value = false;
};

// 编辑
const handleEdit = () => {
	emit('edit', detail.value);
	dialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
.order-detail-drawer {
	:deep(.el-drawer__header) {
		margin-bottom: 0;
		padding: 16px 20px;
		border-bottom: 1px solid var(--el-border-color-lighter);
	}

	:deep(.el-drawer__body) {
		padding: 0;
		overflow: auto;
	}

	:deep(.el-drawer__footer) {
		padding: 16px 20px;
		border-top: 1px solid var(--el-border-color-lighter);
	}
}

.drawer-header {
	display: flex;
	align-items: center;
	gap: 16px;

	.title {
		font-size: 16px;
		font-weight: 500;
		color: var(--el-text-color-primary);
	}

	.order-no {
		.el-tag {
			font-weight: 500;
		}
	}
}

.order-detail {
	padding: 20px;

	.detail-section {
		margin-bottom: 32px;

		&:last-child {
			margin-bottom: 0;
		}

		.section-title {
			font-size: 15px;
			font-weight: 500;
			margin-bottom: 16px;
			padding: 10px 16px;
			background-color: var(--el-fill-color-light);
			border-radius: 4px;
			position: relative;

			&::before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				width: 4px;
				height: 16px;
				background-color: var(--el-color-primary);
				border-radius: 0 2px 2px 0;
			}

			&.bg-\[\#fff3e8\] {
				background-color: var(--el-color-warning-light-9);
				color: var(--el-color-warning-dark-2);
				&::before {
					background-color: var(--el-color-warning);
				}
			}

			&.bg-\[\#e8f7f3\] {
				background-color: var(--el-color-success-light-9);
				color: var(--el-color-success-dark-2);
				&::before {
					background-color: var(--el-color-success);
				}
			}
		}
	}

	:deep(.el-descriptions) {
		.el-descriptions__header {
			display: none;
		}

		.el-descriptions__label {
			width: 120px;
			justify-content: flex-end;
			padding: 12px 16px;
			background-color: var(--el-fill-color-light);
			font-weight: normal;
		}

		.el-descriptions__content {
			padding: 12px 16px;
		}
	}

	.remark-content {
		padding: 12px 16px;
		background-color: var(--el-fill-color-blank);
		border: 1px solid var(--el-border-color-lighter);
		border-radius: 4px;
		min-height: 60px;
		line-height: 1.5;
		color: var(--el-text-color-regular);
	}

	:deep(.el-table) {
		.el-table__header {
			th.el-table__cell {
				background-color: var(--el-fill-color-light);
				color: var(--el-text-color-regular);
				font-weight: 500;
			}
		}

		.el-table__body {
			td.el-table__cell {
				padding: 12px 8px;
			}
		}
	}

	// 运单时间线样式
	.waybill-timeline {
		padding: 16px;
		background-color: var(--el-fill-color-blank);
		border: 1px solid var(--el-border-color-lighter);
		border-radius: 4px;

		.timeline-item {
			display: flex;
			position: relative;
			cursor: pointer;
			padding: 16px 0;

			&:first-child {
				padding-top: 0;
			}

			&:last-child {
				padding-bottom: 0;
			}

			&:hover {
				.timeline-content {
					background-color: var(--el-fill-color-light);
				}
			}

			&.active {
				.timeline-content {
					background-color: var(--el-color-primary-light-9);
				}
				.node-circle {
					background-color: var(--el-color-primary);
					border-color: var(--el-color-primary);
					color: white;
				}
			}

			.timeline-node {
				position: relative;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 0 24px;

				.node-circle {
					width: 32px;
					height: 32px;
					border-radius: 50%;
					border: 2px solid var(--el-border-color);
					background-color: white;
					display: flex;
					align-items: center;
					justify-content: center;
					font-weight: 500;
					color: var(--el-text-color-regular);
					z-index: 1;
				}

				.node-line {
					position: absolute;
					top: 32px;
					width: 2px;
					height: calc(100% - 16px);
					background-color: var(--el-border-color);
				}
			}

			.timeline-content {
				flex: 1;
				padding: 16px;
				border-radius: 4px;
				transition: all 0.3s;

				.waybill-header {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 12px;

					.waybill-no {
						font-size: 15px;
						font-weight: 500;
						color: var(--el-color-primary);
					}
				}

				.waybill-body {
					.info-item {
						display: flex;
						align-items: center;
						margin-bottom: 8px;

						&:last-child {
							margin-bottom: 0;
						}

						.label {
							color: var(--el-text-color-secondary);
							margin-right: 4px;
							min-width: 70px;
						}

						.value {
							color: var(--el-text-color-primary);

							&.time {
								color: var(--el-text-color-secondary);
								font-size: 13px;
							}
						}

						.ml-4 {
							margin-left: 16px;
						}
					}
				}
			}
		}
	}

	// 操作日志样式
	.log-list {
		.log-item {
			display: flex;
			padding: 16px 0;
			border-bottom: 1px solid var(--el-border-color-lighter);

			&:last-child {
				border-bottom: none;
				padding-bottom: 0;
			}

			&:first-child {
				padding-top: 0;
			}

			.log-icon {
				width: 32px;
				height: 32px;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 16px;
				flex-shrink: 0;

				&.create {
					background-color: var(--el-color-success-light-9);
					color: var(--el-color-success);
				}

				&.edit {
					background-color: var(--el-color-warning-light-9);
					color: var(--el-color-warning);
				}

				&.dispatch {
					background-color: var(--el-color-primary-light-9);
					color: var(--el-color-primary);
				}

				&.modify {
					background-color: var(--el-color-info-light-9);
					color: var(--el-color-info);
				}

				&.delete {
					background-color: var(--el-color-danger-light-9);
					color: var(--el-color-danger);
				}

				&.complete {
					background-color: var(--el-color-success-light-9);
					color: var(--el-color-success);
				}
			}

			.log-content {
				flex: 1;

				.log-header {
					margin-bottom: 8px;

					.operator {
						font-weight: 500;
						color: var(--el-text-color-primary);
						margin-right: 12px;
					}

					.time {
						color: var(--el-text-color-secondary);
						font-size: 13px;
					}
				}

				.log-desc {
					color: var(--el-text-color-primary);
					line-height: 1.5;
				}

				.log-remark {
					margin-top: 8px;
					color: var(--el-text-color-secondary);
					font-size: 13px;
				}
			}
		}
	}
}
</style>
